<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="１、面试场景与面试技巧金三银四招聘季，一位粉丝朋友最近在蚂蚁金服第二轮面试时遇到这样一个问题：如果MQ消费遇到瓶颈时该如何处理?。 横向扩容，相比很多读者与我这位朋友一样会脱口而出，面试官显然不会满意这样的回答，然后追问道：横向扩容是堆机器，还有没有其他办法呢？ 在面试过程中，个人建议大家在听到问题后稍作思考，不要立马给出太直接的答案，而是应该与面试官进行探讨，一方面可更深刻的理解面试官的出题初衷">
<meta property="og:type" content="article">
<meta property="og:title" content="剥丝抽茧｜阿里面试题解读：MQ消费端遇到瓶颈该怎么办？">
<meta property="og:url" content="https://www.codingw.net/posts/b86dd57b.html">
<meta property="og:site_name" content="中间件兴趣圈">
<meta property="og:description" content="１、面试场景与面试技巧金三银四招聘季，一位粉丝朋友最近在蚂蚁金服第二轮面试时遇到这样一个问题：如果MQ消费遇到瓶颈时该如何处理?。 横向扩容，相比很多读者与我这位朋友一样会脱口而出，面试官显然不会满意这样的回答，然后追问道：横向扩容是堆机器，还有没有其他办法呢？ 在面试过程中，个人建议大家在听到问题后稍作思考，不要立马给出太直接的答案，而是应该与面试官进行探讨，一方面可更深刻的理解面试官的出题初衷">
<meta property="og:locale">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210315120506640.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210315120528384.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210315120556745.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210315120617963.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center">
<meta property="article:published_time" content="2021-03-15T04:09:34.000Z">
<meta property="article:modified_time" content="2021-04-26T09:59:43.503Z">
<meta property="article:author" content="中间件兴趣圈">
<meta property="article:tag" content="中间件">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20210315120506640.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://www.codingw.net/posts/b86dd57b.html"/>





  <title>剥丝抽茧｜阿里面试题解读：MQ消费端遇到瓶颈该怎么办？ | 中间件兴趣圈</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">中间件兴趣圈</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">微信搜『中间件兴趣圈』，回复『Java』获取200本优质电子书</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://www.codingw.net/posts/b86dd57b.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="中间件兴趣圈">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">剥丝抽茧｜阿里面试题解读：MQ消费端遇到瓶颈该怎么办？</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-03-15T12:09:34+08:00">
                2021-03-15
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/rocketmq/" itemprop="url" rel="index">
                    <span itemprop="name">rocketmq</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/posts/b86dd57b.html" class="leancloud_visitors" data-flag-title="剥丝抽茧｜阿里面试题解读：MQ消费端遇到瓶颈该怎么办？">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <div id="vip-container"><h2 id="１、面试场景与面试技巧"><a href="#１、面试场景与面试技巧" class="headerlink" title="１、面试场景与面试技巧"></a>１、面试场景与面试技巧</h2><p>金三银四招聘季，一位粉丝朋友最近在蚂蚁金服第二轮面试时遇到这样一个问题：<strong>如果MQ消费遇到瓶颈时该如何处理</strong>?。</p>
<p><strong>横向扩容</strong>，相比很多读者与我这位朋友一样会脱口而出，<strong>面试官显然不会满意这样的回答</strong>，然后追问道：<strong>横向扩容是堆机器，还有没有其他办法呢</strong>？</p>
<p>在面试过程中，个人建议大家在听到问题后稍作思考，不要立马给出太直接的答案，而是应该与面试官进行探讨，一方面可更深刻的理解面试官的出题初衷，同时可以给自己梳理一下思路。</p>
<p>消费端遇到瓶颈，<strong>这是一个结果，但引起这个结果的原因是什么呢？</strong>在没有弄清楚原因之前<strong>谈优化、解决方案都会显得很苍白</strong>。</p>
<p>在这样的面试场景中，我们该如何探讨交流呢？我的思路如下：</p>
<ul>
<li>尝试与面试官探讨如何判断消费端遇到瓶颈</li>
<li>如何查找根因 </li>
<li>提出解决方案</li>
</ul>
<blockquote>
<p>温馨提示：为了本文观点的严谨性，本文主要以RocketMQ为例进行剖析。</p>
</blockquote>
<span id="more"></span>

<h2 id="２、如何判断消费端遇到瓶颈"><a href="#２、如何判断消费端遇到瓶颈" class="headerlink" title="２、如何判断消费端遇到瓶颈"></a>２、如何判断消费端遇到瓶颈</h2><p>在RocketMQ消费领域中判断消费端遇到瓶颈通常有两个重要的指标：</p>
<ul>
<li>消息积压数量（延迟数量）</li>
<li>lastConsumeTime</li>
</ul>
<p>在开源版本的控制台rocketmq-console界面中，可以查阅一个消费端上述两个指标，如下图所示：<br><img src="https://img-blog.csdnimg.cn/20210315120506640.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"></p>
<ul>
<li>Delay：消息积压数量，即消费端还剩下多少消息未处理，<strong>该值越大，说明消费端遇到瓶颈了</strong>。</li>
<li>LastConsumeTime：表示上一次成功消费的消息的存储时间，<strong>该值离当前时间越大，同样能说明消费端遇到瓶颈了</strong>。</li>
</ul>
<p>那为什么会积压呢？消费端是在哪遇到瓶颈了呢？</p>
<h2 id="３、如何定位问题"><a href="#３、如何定位问题" class="headerlink" title="３、如何定位问题"></a>３、如何定位问题</h2><p>消费端出现瓶颈，如何识别是客户端的问题还是服务端的问题，一个<strong>最简单的办法</strong>是看集群内其他消费组是否也有积压，特别是和有问题的消费组订阅同一个主题的其他消费组是否有积压，按照笔者的经验，出现消息积压通常是客户端的问题，可以通过查询 rocketmq_client.log加以证明：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grep &quot;flow&quot; rocketmq_client.log</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20210315120528384.png#pic_center" alt="在这里插入图片描述"><br>出现<strong>so do flow control</strong> 这样的日志，说明触发了消费的限流，其直接触发原因：就是消息消费端积压消息，即消费端无法消费已拉取的消息，为了避免内存泄露，RocketMQ在消费端没有将消息处理完成后，不会继续向服务端拉取消息，并打印上述日志。</p>
<p><strong>那如何定位消费端慢在哪呢？是卡在哪行代码呢？</strong></p>
<p>通常的排查方法是跟踪线程栈，即利用jstack命令查看线程运行情况，以此来探究线程的运行情况。</p>
<p>通常使用的命令如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ps -ef | grep java</span><br><span class="line">jstack pid &gt; j1.log</span><br></pre></td></tr></table></figure>

<p>通常为了对比，我一般会连续打印5个文件，从而可以在5个文件中查看同一个消费者线程，其状态是否变化，如果未变化，则说明该线程卡主，那就是我们重点需要关注的地方。</p>
<p>在RocketMQ中，消费端线程以ConsumeMessageThread_开头，通过对线程的判断，如下代码让人为之兴奋：<br><img src="https://img-blog.csdnimg.cn/20210315120556745.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"><br>消费端线程的状态是RUNNABLE，但在5个文件中其状态都是一样，基本可以断定线程卡在具体的代码，从示例代码中是卡在掉一个外部的http借口，从而加以解决，通常在涉及外部调用，特别是http调用，可以设置一个超时时间，避免长时间等待。</p>
<h2 id="4、解决方案"><a href="#4、解决方案" class="headerlink" title="4、解决方案"></a>4、解决方案</h2><p>其实根据第三步骤，大概率能明确是哪个地方慢，遇到了性能瓶颈，通常无非就是调第三方服务，数据库等问题出现了瓶颈，然后对症下药。数据库等性能优化，并不在本文的讨论范围之内，故这里可以点到为止，当然面试官后续可能会继续聊数据库优化等话题，这样就实现了与面试官的交流互动，一环扣一环，技术交流氛围友好，面试通过率大大提高。</p>
<p>最后，我还想和大家探讨一个问题，<strong>出现消息积压就一定意味着遇到消费瓶颈</strong>，一定需要处理吗？</p>
<p><strong>其实也不然</strong>，我们回想一下为什么需要使用MQ，不就是利用异步解耦与<strong>削峰填谷</strong>吗？例如在双十一期间，大量突发流量汇入，此时很可能导致消息积压，这正式我们的用意，用MQ抗住突发流量，后端应用慢慢消费，保证消费端的稳定，在积压的情况下，如果tps正常，即问题不大，<strong>这个时候通常的处理方式就是横向扩容</strong>，尽可能的降低积压，减少业务的延迟。</p>
<p>本文就介绍到这里了。如果大家对RocketMQ感兴趣，可以在CSDN上免费下载由阿里巴巴根据我公众号中RocketMQ专栏整理的电子书：<br><img src="https://img-blog.csdnimg.cn/20210315120617963.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ByZXN0aWdlZGluZw==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"><br>下载链接：<a target="_blank" rel="noopener" href="https://download.csdn.net/download/prestigeding/15769453">RocketMQ电子书下载链接</a></p>
</div>

			<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
			<script>
			var isMobile = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
			if (!isMobile) {
			    var btw = new BTWPlugin();
			    btw.init({
			        "id": "vip-container",
			        "blogId": "18019-1573088808868-542",
			        "name": "中间件兴趣圈",
			        "qrcode": "https://img-blog.csdnimg.cn/20190314214003962.jpg",
			        "keyword": "more"
			    });
			}
			</script>
		
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/b6f85d12.html" rel="next" title="事务消息应用场景、实现原理与项目实战(附全部源码)">
                <i class="fa fa-chevron-left"></i> 事务消息应用场景、实现原理与项目实战(附全部源码)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/6947ca27.html" rel="prev" title="知乎高赞:为什么同样是分布式架构的Kafka需要Leader而Redis不需要">
                知乎高赞:为什么同样是分布式架构的Kafka需要Leader而Redis不需要 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">139</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%EF%BC%91%E3%80%81%E9%9D%A2%E8%AF%95%E5%9C%BA%E6%99%AF%E4%B8%8E%E9%9D%A2%E8%AF%95%E6%8A%80%E5%B7%A7"><span class="nav-number">1.</span> <span class="nav-text">１、面试场景与面试技巧</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%EF%BC%92%E3%80%81%E5%A6%82%E4%BD%95%E5%88%A4%E6%96%AD%E6%B6%88%E8%B4%B9%E7%AB%AF%E9%81%87%E5%88%B0%E7%93%B6%E9%A2%88"><span class="nav-number">2.</span> <span class="nav-text">２、如何判断消费端遇到瓶颈</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%EF%BC%93%E3%80%81%E5%A6%82%E4%BD%95%E5%AE%9A%E4%BD%8D%E9%97%AE%E9%A2%98"><span class="nav-number">3.</span> <span class="nav-text">３、如何定位问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4%E3%80%81%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"><span class="nav-number">4.</span> <span class="nav-text">4、解决方案</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">中间件兴趣圈</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("NNEhOL0iOcflg8f1U3HUqiCq-gzGzoHsz", "7kSmkbbb3DktmHALlShDsBUF");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  

  

  

  

</body>
</html>
